#!/usr/bin/env python
# -*- encoding: UTF-8 -*-
from __future__ import print_function

from flask import render_template, \
    redirect, url_for, flash,request

from . import main
from . import camManger
from . import Cam

#================= 标定 ===============

@main.route('/calibration', methods=['GET', 'POST'])
def calibration():
    #flash(session)
    page_title = u"标定"
    urls = [
        {
            "url": url_for('.select_cam'),
            "text": u"指定相机"
        }]
    btns = []
    cam = camManger.get_current_cam()
    if cam is not None:
        from . import flag
        collect_url = url_for('.calibration_camera_collect', cam_id=cam.id)
        urls.append({"url": collect_url,
                    "text": u"采集及标定"})
    return render_template('index.html',page_title=page_title,urls=urls,btns=btns)

@main.route('/calibration/camera/collect/<int:cam_id>', methods=['GET', 'POST'])
def calibration_camera_collect(cam_id=None):
    """采集单个相机的标定图像"""
    from . import flag
    if not camManger.has(cam_id):
        camManger.add_id(cam_id)
    if "w" in request.values and "h" in request.values:
        w,h = int(request.values['w']),int(request.values['h'])
    else:
        w, h = 12, 9
    btns = [
        {
            "id": "saveImage",
            "name": u"保存图像",
            "api": url_for("api.save_image",cam_id=cam_id,image_flag=flag.image.calibration)
        },
        {
            "id": "Calib_cam",
            "name": u"标定相机",
            "api": url_for("api.calibrate_camera",cam_id=cam_id, w=w, h=h)
        }
    ]
    cam = camManger.get(cam_id=cam_id)
    return render_template('cam.html', cam_name=cam.name, btns=btns,cam_id=cam.id)

